package com.xuhai.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xuhai.book.bean.Publisher;
import com.xuhai.book.mapper.PublisherMapper;
import com.xuhai.book.service.PublisherService;
import com.xuhai.book.util.PageUtil;
import com.xuhai.book.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PublisherServiceImpl implements PublisherService {

    @Autowired
    private PublisherMapper publisherMapper;

    @Override
    public ResultJson getPage(int page, int limit, Publisher publisher) {
        PageHelper.startPage(page,limit);
        List<Publisher> bannerList = publisherMapper.getAll(publisher);
        return ResultJson.ok().data(new PageUtil(new PageInfo<>(bannerList)));
    }

    @Override
    public ResultJson getById(Integer publisherId) {
        return ResultJson.ok().data(publisherMapper.selectByPrimaryKey(publisherId));
    }

    @Override
    public void saveOrUpdate(Publisher publisher) {
        if(publisher.getPublisherId() == null){
            publisherMapper.insertSelective(publisher);
        }else{
            publisherMapper.updateByPrimaryKeySelective(publisher);
        }
    }

    @Override
    public void deleteById(Integer publisherId) {
        publisherMapper.deleteByPrimaryKey(publisherId);
    }
}
